package com.education.module.teacherExamResult;

import java.util.List;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.education.domain.Examdate;
import com.education.domain.TeacherExamResult;
import com.education.domain.TeacherExamSetting;
import com.education.domain.User;
import com.education.framework.base.BaseController;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;
import com.education.framework.session.SessionHelper;
import com.education.module.teacherExamResult.TeacherExamResultServices;
import com.education.framework.util.calendar.CalendarUtil;

@Controller
@RequestMapping("teacherExamResult")
public class TeacherExamResultController extends BaseController{

	@Autowired
	private TeacherExamResultServices services;
	
	@RequestMapping(value = "")
	public String list(Model model, SearchParams searchParams,Page page,ServletRequest request){
		List<TeacherExamSetting> list = services.findResult(searchParams,page);
		List<Examdate> listExam = services.findExamDate();
		model.addAttribute("list", list);
		model.addAttribute("listExam", listExam);
		model.addAttribute("page", page);
		model.addAttribute("searchParams", searchParams);
		return "/teacherExamResult/teacherAssess";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("action","create");
		return "/teacherExamResult/teacherExamResultEdit";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(TeacherExamResult teacherExamResult, Model model,RedirectAttributes redirectAttributes) {
		User sessionUser = SessionHelper.getInstance().getUser();
		teacherExamResult.setCreateUser(sessionUser);
		teacherExamResult.setUpdateUser(sessionUser);
		teacherExamResult.setCreateTime(CalendarUtil.getCurrentDate());
		services.save(teacherExamResult);
		redirectAttributes.addFlashAttribute("teacherExamResult", "SUCCESS");
		return "redirect:/teacherExamResult";
	}
	@RequestMapping(value = "assess", method = RequestMethod.POST)
	public String getResultAssess(TeacherExamResult teacherExamResult, Model model,RedirectAttributes redirectAttributes) {
		List<TeacherExamResult> list = services.findResultDetail(teacherExamResult);
		TeacherExamSetting teacherExamSetting = new TeacherExamSetting();
		if(list.size()>0){
			teacherExamSetting.setTeacherExamResults(list);
			model.addAttribute("teacherName", list.get(0).getTeacherName());
			model.addAttribute("examDateName", list.get(0).getEdName());
		}
		model.addAttribute("teacherExamSetting", teacherExamSetting);
		return "/teacherExamResult/teacherExamResultDetail";
	}
	
	@RequestMapping(value = "saveDeatil", method = RequestMethod.POST)
	public String saveDeatil(TeacherExamSetting teacherExamSetting, RedirectAttributes redirectAttributes) {
		if(null !=teacherExamSetting){
			if(teacherExamSetting.getTeacherExamResults().size()>0){
				for(TeacherExamResult teacherExamResult:teacherExamSetting.getTeacherExamResults()){
					services.updateUserScore(teacherExamResult);
				}
			}
		}
		return "redirect:/teacherExamResult";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Integer id, Model model) {
		TeacherExamResult teacherExamResult = services.findForObject(id);
		model.addAttribute("teacherExamResult", teacherExamResult);
		model.addAttribute("action", "update");
		return "/teacherExamResult/teacherExamResultEdit";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(TeacherExamResult teacherExamResult, RedirectAttributes redirectAttributes) {
		User sessionUser = SessionHelper.getInstance().getUser();
		teacherExamResult.setUpdateUser(sessionUser);
		services.update(teacherExamResult);
		redirectAttributes.addFlashAttribute("teacherExamResult", "SUCCESS");
		return "redirect:/teacherExamResult";
	}
	
	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Integer id,RedirectAttributes redirectAttributes) {
		services.delete(id);
		redirectAttributes.addFlashAttribute("teacherExamResult", "SUCCESS");
		return "redirect:/teacherExamResult";
	}
	
}
